package com.ruoyix.ai.controller;


import org.springframework.ai.image.Image;
import org.springframework.ai.image.ImagePrompt;
import org.springframework.ai.image.ImageResponse;
import org.springframework.ai.openai.OpenAiImageModel;
import org.springframework.ai.openai.OpenAiImageOptions;
import org.springframework.ai.openai.api.OpenAiImageApi;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/ai/image")
public class OpenAiImageController {

    @Autowired
    private OpenAiImageModel openaiImageModel;
    @GetMapping(value = "/generate",produces = "text/html")
    public String genereteImage(String prompt){



        ImageResponse response = openaiImageModel.call(
                new ImagePrompt(prompt,
                        OpenAiImageOptions.builder()
                                .withQuality("hd")
                                .withN(4)
                                .withHeight(1024)
                                .withWidth(1024).build())

        );
        Image output = response.getResult().getOutput();
        //返回图片base64信息
        String b64Json = output.getB64Json();
        //返回图片url信息
        String url = output.getUrl();

        return "<img src='"+url+"'/>";
//        return b64Json;
    }
}
